﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuiltInToURPTool
{
    public class FilesTool
    {
        /// <summary>
        /// 获取路径下所有的shader 文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFiles(string[] paths)
        {
            List<string> filesList = new List<string>();
            foreach (var path in paths)
            {
                CheckFile(path, ref filesList);
            }
            return filesList;
        }

        /// <summary>
        /// 检测路径下shader文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filesList"></param>
        public static void CheckFile(string path,ref List<string> filesList)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            if (Directory.Exists(path))
            {
                DirectoryInfo TheFolder = new DirectoryInfo(path);

                DirectoryInfo[] curAllNextFolder = TheFolder.GetDirectories();

                foreach (var NextFolder in curAllNextFolder)
                {
                    DirectoryInfo[] directory = NextFolder.GetDirectories();
                    FileInfo[] fileInfos = NextFolder.GetFiles();
                    if (fileInfos.Length > 0)
                    {
                        foreach (var file in fileInfos)
                        {
                            if (string.IsNullOrEmpty(file.FullName) == false && file.FullName.Contains(".shader") && file.FullName.Contains(".meta") == false)
                            {
                                filesList.Add(file.FullName);
                            }
                        }
                    }
                    if (directory.Length > 0)
                    {
                        foreach (var item in directory)
                        {
                            CheckFile(item.FullName,ref filesList);
                        }
                    }
                }
                FileInfo[] curfileInfos = TheFolder.GetFiles();
                if (curfileInfos.Length > 0)
                {
                    foreach (var file in curfileInfos)
                    {
                        if (string.IsNullOrEmpty(file.FullName) == false && file.FullName.Contains(".shader") && file.FullName.Contains(".meta") == false)
                        {
                            filesList.Add(file.FullName);
                        }
                    }
                }
            }
            if (File.Exists(path))
            {
                if (string.IsNullOrEmpty(path) == false && path.Contains(".shader") && path.Contains(".meta") == false)
                {
                    filesList.Add(path);
                }
            }
        }
    }
}
